package com.xm.xmapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.CustomLayout;
import com.xm.xmapp.dialog.PilePasswordDialog;
import com.xm.xmapp.dialog.PilePasswordDialog.DissMissable;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Charging_pile_detailRequest;
import com.xm.xmapp.request.bean.Start_chargeRequest;
import com.xm.xmapp.response.bean.ResponseChargingPileList;
import com.xm.xmapp.response.bean.ResponseChargingPileList.E;
import com.xm.xmapp.response.bean.ResponsePileDetail;
import com.xm.xmapp.tools.Tools;

public class PileDetailsActivity extends BaseActivity implements OnClickListener {

	private ResponseChargingPileList.E bean;
	private TextView id, status, parking_fee, charge_electric, charge_service, start_to_charge;
	private CustomLayout time_price;
	private ResponsePileDetail beanResponse;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pile_details);
	}

	@Override
	public void initView() {
		id = (TextView) findViewById(R.id.id);
		status = (TextView) findViewById(R.id.status);
		charge_electric = (TextView) findViewById(R.id.charge_electric);
		parking_fee = (TextView) findViewById(R.id.parking_fee);
		charge_service = (TextView) findViewById(R.id.charge_service);
		start_to_charge = (TextView) findViewById(R.id.start_to_charge);
		time_price = (CustomLayout) findViewById(R.id.time_price);

	}

	@Override
	public void initData() {

		setMyTitle("充电桩详情");
		bean = (E) getIntent().getSerializableExtra("bean");

		if (bean != null) {
			id.setText("编号：" + bean.charging_equipment_interface_id);
			status.setTextColor(0xff666666);
			switch (bean.status) {
			case 1:
			case 2:
				status.setText(bean.desc);
				status.setTextColor(0xffF2cc1c);
				start_to_charge.setOnClickListener(this);
				start_to_charge.setText("立刻充电");
				start_to_charge.setBackgroundResource(R.drawable.onclickbackground1);
				start_to_charge.setTextColor(0xff3c3c3c);
				break;
			case 3:
				status.setText("已被占用了，请选择其它桩。");
				start_to_charge.setOnClickListener(null);
				start_to_charge.setTextColor(0xffffffff);
				start_to_charge.setBackgroundResource(R.drawable.myborder4);
				break;
			case 0:
			case 4:
			case 255:

				status.setText("暂不可用，维护中。");
				start_to_charge.setOnClickListener(null);
				start_to_charge.setTextColor(0xffffffff);
				start_to_charge.setBackgroundResource(R.drawable.myborder4);
				break;
			default:
				break;
			}

			getData();
		} else {
			Tools.showMessage("程序异常！");
		}

	}

	private void getData() {

		Charging_pile_detailRequest requestBean = new Charging_pile_detailRequest();
		requestBean.charging_equipment_interface_id = bean.charging_equipment_interface_id;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();

				beanResponse = JSON.parseObject(response, ResponsePileDetail.class);

				parking_fee.setText("停车费：" + beanResponse.parking_fee);

				if (beanResponse.first_price != null) {
					time_price.setText1("时    段   " + beanResponse.first_price.duration_start + " ~ "
							+ beanResponse.first_price.duration_end);
					charge_electric.setText("电    费   " + beanResponse.first_price.charge_electric + "元/度");
					charge_service.setText("服务费   " + beanResponse.first_price.charge_service + "元/度");
				} else {
					time_price.setText1("时    段   " + beanResponse.price.get(0).duration_start + " ~ "
							+ beanResponse.price.get(0).duration_end);
					charge_electric.setText("电    费   " + beanResponse.price.get(0).charge_electric + "元/度");
					charge_service.setText("服务费   " + beanResponse.price.get(0).charge_service + "元/度");
				}

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

	@Override
	public void initEvent() {
		time_price.setOnClickListener(PileDetailsActivity.this);
	}

	private void startCharge() {

		DissMissable mDissMissable = new DissMissable() {

			@Override
			public void getPsw(String pwd) {
				Start_chargeRequest requestBean = new Start_chargeRequest();
				requestBean.chargeType = 2;
				requestBean.connectorId = bean.charging_equipment_interface_id;
				requestBean.userPin = pwd;
				promptDialog.show();
				GetDataInterFace<String> listener = new GetDataInterFace<String>() {

					@Override
					public void onResponse(String response) {
						promptDialog.dismiss();
					}
				};

				MyAppcation.getMyAppcation().getPostData(PileDetailsActivity.this, requestBean, listener,
						new DisssmissInterFace() {

							@Override
							public void putError(VolleyError error) {
								promptDialog.dismiss();
							}
						});
			}

		};
		PilePasswordDialog passwordDialog = new PilePasswordDialog(this, mDissMissable);
		passwordDialog.show();

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.start_to_charge:
			startCharge();
			break;
		case R.id.time_price:

			if (beanResponse != null && beanResponse.price != null && beanResponse.price.size() > 0) {
				intent = new Intent(getApplicationContext(), TimePriceActivity.class);
				intent.putExtra("bean", beanResponse);
				startActivity(intent);
			} else {
				getData();
			}

			break;

		default:
			break;
		}

	}

}
